#include "stack.h"

#include <stdlib.h>
#include <wchar.h>
#include "utils/assert.h"
// #include "utils/logger.h"

StackNode *createStackNode(const wchar_t *data)
{
    StackNode *node = (StackNode *)malloc(sizeof(StackNode));
    node->data = (wchar_t *)malloc(sizeof(wchar_t) * wcslen(data) + 1);
    wcscpy(node->data, data);
    node->previous = NULL;
    node->next = NULL;
    return node;
}

void deleteStackNode(StackNode *node)
{
    free(node->data);
    free(node);
}

StackNode *stack_push(StackNode *top, const wchar_t *data)
{
    StackNode *newTop = createStackNode(data);
    if (NULL == top)
    {
        return newTop;
    }
    ASSERT(top->next == NULL);
    top->next = newTop;
    newTop->previous = top;
    return newTop;
}

StackNode *stack_pop(StackNode *top)
{
    ASSERT(top != NULL);
    StackNode *previousNode = top->previous;
    if (previousNode)
    {
        ASSERT(previousNode->next == top);
        previousNode->next = NULL;
    }

    deleteStackNode(top);
    return previousNode;
}

void stack_delete(StackNode *top)
{
    while (top)
    {
        top = stack_pop(top);
    }
}